//
//  WBWelcomeViewController.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/20.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit

class WBWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
    }
    
    // MARK: -  当界面显示出来之后调用该方法
    override func viewDidAppear(animated: Bool) {
        
        headImageViewAnimate()
    }
    
    // MARK: -  头像动画
    private func headImageViewAnimate() {
        
        headImageView.snp_updateConstraints { (make) -> Void in
            
            make.top.equalTo(view).offset(100)
        }
        
        //设置动画
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            //刷新界面
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.05, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchViewController, object: "welcome")
                })
        }
    }
    
    
    // MARK: -  设置界面视图
    private func setUpUI() {
        
        view.addSubview(headImageView)
        view.addSubview(welcomeLabel)
        
        setUIFrame()
    }
    
    // MARK: -  设置界面控件的布局
    private func setUIFrame() {
        
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.top.equalTo(view).offset(400)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
            
        }
    }
    
    // MARK: -  loadView
    override func loadView() {
        view = backgroundImageView
    }
    
    // MARK: -  懒加载头像
    private lazy var headImageView: UIImageView = {
        
        let headImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        headImageView.layer.cornerRadius = 85 / 2
        headImageView.layer.masksToBounds = true
        headImageView.layer.borderColor = themeColor.CGColor
        headImageView.layer.borderWidth = 2
        headImageView.sd_setImageWithURL(NSURL(string: (WBUserAccountTool.sharedAccountTool.userAccount?.avatar_large)!), placeholderImage: UIImage(named: "avatar_default_big"))
        return headImageView
    }()

    // MARK: -  懒加载背景图片
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // MARK: -  懒加载label
    private lazy var welcomeLabel: UILabel = {
        
        let welcomeLabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 17)
        welcomeLabel.textAlignment = .Center
        welcomeLabel.text = "欢迎回来"
        welcomeLabel.alpha = 0
        return welcomeLabel
    }()
    
}
